<?php
declare (strict_types=1);

namespace app\admin\controller;

use think\App;
use think\facade\Cache;
use addons\Builder\facade\Builder;

class Logs extends Base
{

    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\Logs';

    //关联模型
    protected array $hasOne = [];

    public function index(): string
    {
        return Builder::Table()
            ->addColumn(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addColumn(['field' => 'emp_name', 'title' => '操作人'])
            ->addColumn(['title' => '操作IP', 'templet' => '#logTpl'])
            ->addColumn(['field' => 'event', 'title' => '操作方法'])
            ->addColumn(['field' => 'description', 'title' => '描述', 'templet' => '<div>{{- d.description}}</div>'])
            ->addColumn(['field' => 'create_time', 'title' => '时间', 'fixed' => 'right', 'align' => 'center'])
            ->addSearch([
                ['emp_name__ip', '会员编号/管理员账号/IP地址查询', 'input'],
                ['description', '输入关键字筛选', 'input,Like']
            ])
            ->addJsUrl(['logs'])
            ->addExtraHtml(['logs'])
            ->fetch();
    }

    public function queryIp($ip): \think\response\Json
    {
        if ($ip == '127.0.0.1') {
            Cache::set('ipAddress_127.0.0.1', '本地IP');
            return json(['code' => 1, 'msg' => '查询成功', 'result' => '本地IP']);
        }
        $res = queryIp($ip);
        if ($res) {
            Cache::set('ipAddress_' . $ip, $res);
            return json(['code' => 1, 'msg' => '查询成功', 'result' => $res]);
        } else {
            return json(['code' => -1, 'msg' => '查询失败']);
        }
    }

}
